/**
 * Copyright (C) 2018-2022
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制
 */
package co.yixiang.controller.manager.wechat;

import co.yixiang.common.exception.BadRequestException;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.mp.entity.domain.YxHelpCategory;
import co.yixiang.modules.mp.entity.dto.YxHelpCategoryDto;
import co.yixiang.modules.mp.entity.dto.YxHelpCategoryQueryCriteria;
import co.yixiang.modules.mp.service.YxHelpCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author hupeng
 * @date 2019-10-07
 */
@Api(tags = "商城:帮助分类管理")
@RestController
@RequestMapping("api")
public class WechatHelpCategoryController {

    private final YxHelpCategoryService helpCategoryService;
    private final IGenerator generator;

    private static final String ENTITY_NAME = "category";

    public WechatHelpCategoryController(YxHelpCategoryService helpCategoryService, IGenerator generator) {
        this.helpCategoryService = helpCategoryService;
        this.generator = generator;
    }

    @ApiOperation("导出文章分类数据")
    @GetMapping(value = "/yxHelpCategory/download")
    @PreAuthorize("hasAnyRole('admin','common','common','common','YXHELPCATEGORY_ALL','YXHELPCATEGORY_SELECT')")
    public void download(HttpServletResponse response, YxHelpCategoryQueryCriteria criteria) throws IOException {

        helpCategoryService.download(generator.convert(helpCategoryService.queryAll(criteria), YxHelpCategoryDto.class), response);
    }

    @ApiOperation("获取文章分类")
    @GetMapping(value = "/yxHelpCategory/build")
    @PreAuthorize("hasAnyRole('admin','common','common','common','YXHELPCATEGORY_ALL','YXHELPCATEGORY_SELECT')")
    public ResponseEntity<Object> buildYxHelpCategorys() {
        final List<YxHelpCategoryDto> convert = generator.convert(helpCategoryService.queryAll(null), YxHelpCategoryDto.class);
        List<YxHelpCategoryDto> YxHelpCategoryDtos = (List<YxHelpCategoryDto>) helpCategoryService.buildTree(convert).get("content");
        return new ResponseEntity<>(helpCategoryService.buildYxHelpCategorys(YxHelpCategoryDtos), HttpStatus.OK);
    }

    @ApiOperation("返回全部的菜单")
    @GetMapping(value = "/yxHelpCategory/tree")
    @PreAuthorize("hasAnyRole('admin','common','common','common','YXHELPCATEGORY_ALL','YXHELPCATEGORY_SELECT')")
    public ResponseEntity<Object> getYxHelpCategoryTree() {
        return new ResponseEntity<>(helpCategoryService.getYxHelpCategoryTree(helpCategoryService.findByPid(0L)), HttpStatus.OK);
    }

    @ApiOperation("查询菜单")
    @GetMapping(value = "/yxHelpCategory")
    //@PreAuthorize("hasAnyRole('admin','common','common','YXHELPCATEGORY_ALL','YXHELPCATEGORY_SELECT')")
    public ResponseEntity<Object> getYxHelpCategorys(YxHelpCategoryQueryCriteria criteria) {
        List<YxHelpCategoryDto> YxHelpCategoryDtoList = generator.convert(helpCategoryService.queryAll(criteria), YxHelpCategoryDto.class);
        return new ResponseEntity<>(helpCategoryService.buildTree(YxHelpCategoryDtoList), HttpStatus.OK);
    }


    @ApiOperation("新增菜单")
    @PostMapping(value = "/yxHelpCategory")
    @PreAuthorize("hasAnyRole('admin','common','common','YXHELPCATEGORY_ALL','YXHELPCATEGORY_CREATE')")
    public ResponseEntity<Object> create(@Validated @RequestBody YxHelpCategory resources) {

        if (resources.getId() != null) {
            throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
        }

        return new ResponseEntity<>(helpCategoryService.create(resources), HttpStatus.CREATED);
    }


    @ApiOperation("修改菜单")
    @PutMapping(value = "/yxHelpCategory")
    @PreAuthorize("hasAnyRole('admin','common','common','YXHELPCATEGORY_ALL','YXHELPCATEGORY_EDIT')")
    public ResponseEntity<Object> update(@Validated @RequestBody YxHelpCategory resources) {

        helpCategoryService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ApiOperation("删除菜单")
    @DeleteMapping(value = "/yxHelpCategory")
    @PreAuthorize("hasAnyRole('admin','common','common','YXHELPCATEGORY_ALL','YXHELPCATEGORY_DELETE')")
    public ResponseEntity<Object> delete(@RequestBody Set<Long> ids) {

        Set<YxHelpCategory> YxHelpCategorySet = new HashSet<>();
        for (Long id : ids) {
            List<YxHelpCategory> YxHelpCategoryList = helpCategoryService.findByPid(id);
            YxHelpCategorySet.add(helpCategoryService.getOne(new LambdaQueryWrapper<YxHelpCategory>().eq(YxHelpCategory::getId, id)));
            YxHelpCategorySet = helpCategoryService.getDeleteYxHelpCategorys(YxHelpCategoryList, YxHelpCategorySet);
        }
        helpCategoryService.delete(YxHelpCategorySet);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
